%% PREPARATION OF THE DATABASE
% by IMF Institute for Capacity Development - Course on Monetary and Exchange Rate Policies

%% Clear the workspace
close all;
clear all;
disp('Working ...');

%% Load quarterly data
% Command 'dbdload' loads the data from the 'csv' file (save from Excel as
% .csv in the current directory). All the data are now available in the
% database 'd' 
d = dbload('data.csv');

%% Seasonal adjustment
list = dbnames(d);

for i = 1:length(list)
    if length(list{i})>1
        if strcmp('_u', list{i}(end-1:end))
            d.(list{i}(1:end-2)) = x12(d.(list{i}), Inf, 'mode', 'm');
            d = rmfield(d, list{i});
        end
    end
end

%% Make log of variables
exceptions = {'rn','x_rn','rn1w','rnon'};

list = dbnames(d);

for i = 1:length(list)
    if isempty(strmatch(list{i},exceptions,'exact'))
        d.(['l' list{i}]) = 100*log(d.(list{i}));
    end
end

%% Define the real exchange rate
d.lz = d.ls + d.lx_cpi - d.lcpi;

%% Growth rate qoq, yoy
exceptions = {'rn','x_rn','rn1w','rnon'};

list = dbnames(d);

for i = 1:length(list)
    if isempty(strmatch(list{i}, exceptions,'exact'))
        if length(list{i})>1
            if strcmp('l', list{i}(1:1))
                d.(['dot_' list{i}(2:end)])  = 4*(d.(list{i}) - d.(list{i}){-1});
                d.(['dot4_' list{i}(2:end)]) = d.(list{i}) - d.(list{i}){-4};
            end
        end
    end
end


%% Real variables
% Domestic real interest rate
d.rr = d.rn - d.dot4_cpi;

% Fooreign real interest rate
d.x_rr = d.x_rn - d.dot4_x_cpi;

%% Trends and Gaps - Hodrick-Prescott filter
list = {'rr','lz','x_rr','lgdp','lx_gdp'};

for i = 1:length(list)
    [d.([list{i} '_eq']), d.([list{i} '_gap'])] = hpf(d.(list{i}));
end

d.dot_z_eq = 4*(d.lz_eq - d.lz_eq{-1});
d.dot_gdp_eq = 4*(d.lgdp_eq - d.lgdp_eq{-1});

%% Trend and Gap for Output - Band-pass filter
d.lgdp_gap_bp = bpass(d.lgdp,inf,[6,32],'detrend',false);
d.lgdp_eq_bp = hpf((d.lgdp - d.lgdp_gap_bp),inf,'lambda',5);
d.dot_gdp_eq_bp = 4*(d.lgdp_eq_bp - d.lgdp_eq_bp{-1});

%% Foreign Output gap - Band-pass filter
d.lx_gdp_gap_bp = bpass(d.lx_gdp,inf,[6,32],'detrend',false);

%% Inflation target (history)
d.target = tseries(get(d.dot_cpi,'first2last'),6);
% d.target = hpf(d.dot_cpi);

% %If you want use a different number for the end value (there is 10 as a proxy) change the first lines 
%prior_cpi = tseries(get(d.dot_cpi,'end'),10);
% d.target = hpf(d.dot_cpi,inf,'level',prior_cpi);

%% Compute the exchange rate target over the history
%Exchange rate target is automatically calculated as to equal observed
%exchange rate at the end of the sample
prior_ls_tar = tseries(get(d.ls,'last'),d.ls(end));
d.ls_tar = hpf(d.ls,inf,'lambda',1600,'level',prior_ls_tar);
d.dot_s_tar = 4*(d.ls_tar - d.ls_tar{-1});

%% Real marginal costs
p.b3 = 0.5;
d.rmc = p.b3*d.lgdp_gap + (1-p.b3)*d.lz_gap;

%% Real monetary conditions
p.a4 = 0.6;
d.mci = p.a4*d.rr_gap + (1-p.a4)*(-d.lz_gap);

%% Expert change in the database--equilibrium real interest rate
d.shock_prem = tseries(get(d.ls,'range'),0);
% d.rr_eq(qq(2009,1)) = 1;

%% Expert change in the database--output gap
% d.lgdp_gap(qq(2012,4):qq(2012,4)) = -5;
% d.lz_gap(qq(2013,1):qq(2013,1)) = 0;

%% Sets cumulative output gap zero at the end of the observed sample
d.cum_gap = tseries(get(d.lgdp,'first2last'),0);

%% Save the database
% Database is saved in file 'history.csv'
dbsave(d,'history.csv');

% Report
x = report.new('Georgia - Stylized Facts','visible',true);

% Figures
rng = qq(2004,1):get(d.dot4_cpi,'last');

sty = struct();
sty.line.linewidth = 1;
sty.line.linestyle = {'-';'--'};
sty.line.color = {'k';'k'};
sty.axes.box = 'off';
sty.legend.location='southOutside';
sty.legend.orientation = 'horizontal';

x.figure('Nominal Variables','subplot',[2,2],'style',sty,'range',rng,'dateformat','YY:P');

x.graph('Inflation','legend',true);
x.series({'q-o-q','y-o-y'},[d.dot_cpi d.dot4_cpi]);

x.graph('Nominal Interest Rate','legend',false);
x.series('',[d.rn]);

x.graph('Nominal Exchange Rate - GEL/USD','legend',true);
x.series({'q-o-q','y-o-y'},[d.dot_s d.dot4_s]);

x.graph('Foreign Inflation (q-o-q)','legend',false);
x.series('',[d.dot_x_cpi]);

x.pagebreak();

x.figure('Real Variables and Trends','subplot',[3,2],'style',sty,'range',rng,'dateformat','YY:P');

x.graph('GDP Growth','legend',true);
x.series({'q-o-q','y-o-y'},[d.dot_gdp d.dot4_gdp]);

x.graph('GDP','legend',false);
x.series('',[d.lgdp d.lgdp_eq]);

x.graph('Real Interest Rate','legend',false);
x.series('',[d.rr d.rr_eq]);

x.graph('Foreign Real Interest Rate','legend',false);
x.series('',[d.x_rr d.x_rr_eq]);

x.graph('Real Exchange Rate','legend',false);
x.series('',[d.lz d.lz_eq]);

x.graph('Change in the Real Eq. Exchange Rate (q-o-q)','legend',false);
x.series('',[d.dot_z_eq]);

x.pagebreak();

x.figure('Gaps','subplot',[2,2],'style',sty,'range',rng,'dateformat','YY:P');

x.graph('GDP Gap','legend',false);
x.series('',[d.lgdp_gap]);

x.graph('Real Interest Rate Gap','legend',false);
x.series('',[d.rr_gap]);

x.graph('Real Exchange Rate Gap','legend',false);
x.series('',[d.lz_gap]);

x.graph('Foreign Output Gap','legend',false);
x.series('',[d.lx_gdp_gap]);
  

%% Make the report
x.publish('Stylized_facts.pdf','display',false);
disp('Done!');